class Chalk:
    text_arr = []
    display_mode = 1
    bg_color_code = 0
    text_color_code = 0
    line_tpl = ' \033[31m●\033[0m {} '

    def __init__(self, text=''):
        if text != '':
            self.text_arr = [{'text': text}]
        else:
            self.text_arr = []

    def set_display_mode(self, mode):
        self.display_mode = mode
        return self

    def set_bg_color(self, code):
        self.bg_color_code = code
        return self

    def add_text(self, text, color_code):
        param = {'text': text}
        if color_code in range(30, 38):
            param.update({'color': color_code})
        if self.bg_color_code in range(40, 48):
            param.update({'bg_color': self.bg_color_code})
        if self.display_mode in [0, 1, 4, 5, 7, 8]:
            param.update({'mode': self.display_mode})
        self.text_arr.append(param)

    def build_color_text(self):
        color_text_arr = []
        for text_item in self.text_arr:
            text = text_item.get('text')
            symbol_arr = []
            mode = text_item.get('mode', None)
            color = text_item.get('color', None)
            bg_color = text_item.get('bg_color', None)
            if mode is not None:
                symbol_arr.append(str(mode))
            if color is not None:
                symbol_arr.append(str(color))
            if bg_color is not None:
                symbol_arr.append(str(bg_color))
            symbols_str = ';'.join(symbol_arr)
            if symbols_str != '':
                line_text = '\033[{}m{}\033[0m'.format(symbols_str, text)
            else:
                line_text = '{}'.format(text)
            color_text_arr.append(self.line_tpl.format(line_text))
        return color_text_arr

    def br(self):
        self.text_arr.append({'text': '\n'})
        return self

    def red(self, text):
        code = 31
        self.add_text(text, code)
        return self

    def green(self, text):
        code = 32
        self.add_text(text, code)
        return self

    def yellow(self, text):
        code = 33
        self.add_text(text, code)
        return self

    def blue(self, text):
        code = 34
        self.add_text(text, code)
        return self

    def magenta(self, text):
        code = 35
        self.add_text(text, code)
        return self

    def cyan(self, text):
        code = 36
        self.add_text(text, code)
        return self

    def white(self, text):
        code = 37
        self.add_text(text, code)
        return self

    def echo(self):
        color_text_arr = self.build_color_text()
        text = ''.join(color_text_arr)
        print(text)
